package cc.io24.knewcafe.boss.service;

import cc.io24.knewcafe.boss.domain.JobDetailDo;
import cc.io24.knewcafe.boss.repository.BaseRepository;
import cc.io24.knewcafe.boss.vo.JobDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;


public class JobDetailService {


    private BaseRepository jobDetailDao;

    public boolean startJob(JobDetail jobDetail){
        JobDetailDo domain = new JobDetailDo();
        BeanUtils.copyProperties(jobDetail,domain);
        jobDetailDao.save(domain);
        return true;
    }

    public boolean finishJob(JobDetail jobDetail){
        JobDetailDo domain = new JobDetailDo();
        BeanUtils.copyProperties(jobDetail,domain);
        jobDetailDao.save(domain);
        return true;
    }

    public List<JobDetail> findList(JobDetail jobDetail,Pageable pageable){
        //_sort=title&_order=ASC&_start=0&_end=24&title=bar
        JobDetailDo jobDetailDo = new JobDetailDo();
        BeanUtils.copyProperties(jobDetail,jobDetailDo);
        Example<JobDetailDo> example = Example.of(jobDetailDo);


        Page<JobDetailDo> jobDetailDos = jobDetailDao.findAll(example,pageable);
        List<JobDetail> jobDetails = new ArrayList<JobDetail>();

        for (JobDetailDo jobDetailDo1:jobDetailDos){
            JobDetail jobDetail1 = new JobDetail();
            BeanUtils.copyProperties(jobDetailDo1,jobDetail1);
            jobDetails.add(jobDetail1);
        }

        return jobDetails;
    }

    public void setJobDetailDao(BaseRepository jobDetailDao) {
        this.jobDetailDao = jobDetailDao;
    }
}
